using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
using Microsoft.Win32;

namespace DLLViewer
{
    public partial class MainWindow : Window
    {
        private readonly DecompilerService _decompilerService;
        private string _currentFilePath;

        public MainWindow()
        {
            InitializeComponent();
            _decompilerService = new DecompilerService();
            
            // 支持拖拽
            this.AllowDrop = true;
            this.Drop += MainWindow_Drop;
            this.DragOver += MainWindow_DragOver;
        }

        private void MainWindow_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files.Length == 1 && Path.GetExtension(files[0]).ToLower() == ".dll")
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                }
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
            e.Handled = true;
        }

        private void MainWindow_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files.Length > 0)
                {
                    LoadAssembly(files[0]);
                }
            }
        }

        private void OpenFileButton_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter = "DLL Files (*.dll)|*.dll|All Files (*.*)|*.*",
                Title = "选择 DLL 文件"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                LoadAssembly(openFileDialog.FileName);
            }
        }

        private void LoadAssembly(string filePath)
        {
            try
            {
                _currentFilePath = filePath;
                FileNameText.Text = Path.GetFileName(filePath);
                
                var treeItems = _decompilerService.LoadAssembly(filePath);
                AssemblyTreeView.ItemsSource = treeItems;
                
                Title = $"DLL 可视化反编译工具 - {Path.GetFileName(filePath)}";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载 DLL 失败: {ex.Message}", "错误", 
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void AssemblyTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is TreeViewItem selectedItem && selectedItem.Tag != null)
            {
                try
                {
                    var language = LanguageComboBox.SelectedIndex == 0 ? "C#" : "C++";
                    var code = _decompilerService.GetCode(selectedItem.Tag, language);
                    CodeEditor.Text = code;
                }
                catch (Exception ex)
                {
                    CodeEditor.Text = $"// 反编译失败: {ex.Message}";
                }
            }
        }

        private void LanguageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AssemblyTreeView.SelectedItem is TreeViewItem selectedItem && selectedItem.Tag != null)
            {
                try
                {
                    var language = LanguageComboBox.SelectedIndex == 0 ? "C#" : "C++";
                    var code = _decompilerService.GetCode(selectedItem.Tag, language);
                    CodeEditor.Text = code;
                }
                catch (Exception ex)
                {
                    CodeEditor.Text = $"// 反编译失败: {ex.Message}";
                }
            }
        }
    }
}
